package cn.stylefeng.guns.modular.ry600.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import cn.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.stylefeng.guns.modular.system.model.Clientcustomer;
import cn.stylefeng.guns.modular.system.model.Clientproduct;
import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.ry600.service.IClientcustomerService;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;

/**
 * 连锁门店管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-13 20:14:17
 */
@Controller
@RequestMapping("/clientcustomer")
public class ClientcustomerController extends BaseController {

	private String PREFIX = "/ry600/clientcustomer/";

	@Autowired
	private IClientcustomerService clientcustomerService;
	@Autowired
	private IErpkindService erpkindService;

	/**
	 * 跳转到连锁门店管理首页
	 */
	@RequestMapping("")
	public String index() {
		return PREFIX + "clientcustomer.html";
	}

	/**
	 * 跳转到添加连锁门店管理
	 */
	@RequestMapping("/clientcustomer_add")
	public String clientcustomerAdd() {
		return PREFIX + "clientcustomer_add.html";
	}

	/**
	 * 跳转到修改连锁门店管理
	 */
	@RequestMapping("/clientcustomer_update/{clientcustomerId}")
	public String clientcustomerUpdate(@PathVariable Integer clientcustomerId,
			Model model) {
		Clientcustomer clientcustomer = clientcustomerService
				.selectById(clientcustomerId);
		model.addAttribute("item", clientcustomer);
		LogObjectHolder.me().set(clientcustomer);
		return PREFIX + "clientcustomer_edit.html";
	}

	/**
	 * 获取连锁门店管理列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(String condition) {
		return clientcustomerService.selectList(null);
	}

	/**
	 * 新增连锁门店管理
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(Clientcustomer clientcustomer) {
		clientcustomerService.insert(clientcustomer);
		return SUCCESS_TIP;
	}

	/**
	 * 删除连锁门店管理
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Integer clientcustomerId) {
		List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientcustomerService.deleteByIdProxy(clientcustomerId,
						resultErpKind.get(j).getDataSource());
			}
		}
		return SUCCESS_TIP;
	}
	
	
	@RequestMapping(value = "/delByDao")
	@ResponseBody
	public Object delByDao(@RequestParam(required=false) String  clientId,@RequestParam(required=false) String  orgId) {
		Clientcustomer clientcustomer=new Clientcustomer();
		clientcustomer.setClientId(clientId);
		clientcustomer.setOrgId(orgId);
		List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientcustomerService.delByDao(clientcustomer,
						resultErpKind.get(j).getDataSource());
			}
		}
		return SUCCESS_TIP;
	}

	/**
	 * 修改连锁门店管理
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Object update(Clientcustomer clientcustomer) {
		clientcustomerService.updateById(clientcustomer);
		return SUCCESS_TIP;
	}

	/**
	 * 连锁门店管理详情
	 */
	@RequestMapping(value = "/detail/{clientcustomerId}")
	@ResponseBody
	public Object detail(
			@PathVariable("clientcustomerId") Integer clientcustomerId) {
		return clientcustomerService.selectById(clientcustomerId);
	}

	@RequestMapping(value = "/selectByMsg")
	@ResponseBody
	public Object selectByMsg(HttpServletRequest request,
			HttpServletResponse response) {
		Integer page = Integer.valueOf(request.getParameter("page"));
		Integer rows = Integer.valueOf(request.getParameter("rows"));
		Map<String, Object> msg = this.setMap(request);
		PageHelper.startPage(page, rows);
		List<Clientcustomer> result = (List<Clientcustomer>) clientcustomerService.selectByMsg(msg);
		PageInfo<Clientcustomer> pageInfo = new PageInfo<Clientcustomer>(result);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return map;

	}

	public Map<String, Object> setMap(HttpServletRequest request) {
		Map<String, Object> msg = new HashMap<String, Object>();
		String searchKey = request.getParameter("searchKey");
		String searchWord = request.getParameter("searchWord");
		String searchKey1 = request.getParameter("searchKey1");
		String searchWord1 = request.getParameter("searchWord1");

		if (!StringUtils.isBlank(searchKey1)
				&& !StringUtils.isBlank(searchWord1)) {
			msg.put(searchKey1.trim(), searchWord1.trim());
		}
		if (!StringUtils.isBlank(searchKey) && !StringUtils.isBlank(searchWord)) {
			msg.put(searchKey.trim(), searchWord.trim());
		}
		return msg;
	}

	@RequestMapping(value = "/batchadd")
	@ResponseBody
	public Object batchadd(String json) {
		// List<Clientcustomer> clientcustomers=(List<Clientcustomer>)
		// JSON.parse(json);
		List<Clientcustomer> clientcustomers = JSON.parseArray(json,
				Clientcustomer.class);
		List<Erpkind> resultErpKind = erpkindService.selectList(null);
		Set<String> dataSource = new HashSet<String>();
		for (int j = 0; j < resultErpKind.size(); j++) {
			if (dataSource.add(resultErpKind.get(j).getDataSource())) {
				clientcustomerService.insertBatch(clientcustomers,
						resultErpKind.get(j).getDataSource());
			}
		}
		return SUCCESS_TIP;
	}

}
